package com.cailu.rabmq.sender.swagger;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cailu.rabmq.sender.version.ApiVersionConstant;
import com.cailu.rabmq.sender.version.ApiVersions;

/**
 * @author cailu
 * @filename swagger配置
 * @description SwaggerApplication
 * @date 2019/11/26 10:53
 */
@EnableSwagger2
@Configuration
public class SwaggerApplication {

	@Value("${swagger.host}")
	private String swaggerHost;

	@Value("${swagger.show}")
	private boolean swaggerShow;

	@Bean
	public Docket customDocket() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		if (StringUtils.isNotBlank(swaggerHost)) {
			docket = docket.host(swaggerHost);
		}
		docket = docket.apiInfo(apiInfo()).enable(swaggerShow)
				.useDefaultResponseMessages(false)
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.cailu.rabmq.sender.controller"))
				.paths(PathSelectors.any())
				.build()
//				.pathMapping("/doc")
				.apiInfo(apiInfo());

		return docket;
	}

	@Bean
	public Docket vApi100() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		if (StringUtils.isNotBlank(swaggerHost)) {
			docket = docket.host(swaggerHost);
		}
		docket = docket.apiInfo(apiInfo()).enable(swaggerShow)
				.groupName(ApiVersionConstant.FAP_APP100)
				.useDefaultResponseMessages(false)
				.select()
				.apis(input -> {
					ApiVersions apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersions.class);
					if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.FAP_APP100)) {
						return true;
					}
					return false;
				})
				.paths(PathSelectors.any())
				.build()
				//				.pathMapping("/doc")
				.apiInfo(apiInfo());

		return docket;
	}

	@Bean
	public Docket vApi200() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		if (StringUtils.isNotBlank(swaggerHost)) {
			docket = docket.host(swaggerHost);
		}
		docket = docket.apiInfo(apiInfo()).enable(swaggerShow)
				.groupName(ApiVersionConstant.FAP_APP200)
				.useDefaultResponseMessages(false)
				.select()
				.apis(input -> {
					ApiVersions apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersions.class);
					if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.FAP_APP200)) {
						return true;
					}
					return false;
				})
				.paths(PathSelectors.any())
				.build()
				//				.pathMapping("/doc")
				.apiInfo(apiInfo());

		return docket;
	}

	@Bean
	public Docket vApi300() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		if (StringUtils.isNotBlank(swaggerHost)) {
			docket = docket.host(swaggerHost);
		}
		docket = docket.apiInfo(apiInfo()).enable(swaggerShow)
				.groupName(ApiVersionConstant.FAP_APP300)
				.useDefaultResponseMessages(false)
				.select()
				.apis(input -> {
					ApiVersions apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersions.class);
					if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.FAP_APP300)) {
						return true;
					}
					return false;
				})
				.paths(PathSelectors.any())
				.build()
				//				.pathMapping("/doc")
				.apiInfo(apiInfo());

		return docket;
	}


	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("api文档").description("简单优雅的restfun风格").contact("cailu")
				.termsOfServiceUrl("750415222@qq.com")
				.version("1.0")
				.build();
	}

}
